package com.ars.website;

import com.ars.db.DBTools;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

import javax.swing.*;
import java.net.URL;
import java.sql.ResultSet;
import java.util.ResourceBundle;

public class ShowWebsiteController implements Initializable {
    DBTools db = null;
    @FXML
    private TableView tableView;
    @FXML
    private TableColumn websiteNameColumn;
    @FXML
    private TableColumn websiteCityColumn;
    @FXML
    private TableColumn websiteAddressColumn;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        websiteNameColumn.setCellValueFactory(new PropertyValueFactory<>("WebsiteName"));
        websiteCityColumn.setCellValueFactory(new PropertyValueFactory<>("WebsiteCity"));
        websiteAddressColumn.setCellValueFactory(new PropertyValueFactory<>("WebsiteAddress"));

        tableView.setItems(getData());
    }

    private ObservableList getData() {
        ObservableList data = FXCollections.observableArrayList();
        String sql = "select * from website where `limit`='3'";
        db = new DBTools();
        ResultSet rs = db.query(sql);
        try {
            while (rs.next()) {
                Website website = new Website();
                website.setWebsiteName(rs.getString("name"));
                website.setWebsiteCity(rs.getString("city"));
                website.setWebsiteAddress(rs.getString("address"));

                data.add(website);
            }
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "获取信息失败！", "错误", JOptionPane.ERROR_MESSAGE);
        } finally {
            db.close();
        }
        return data;
    }


}
